package com.uliky.shouyiLibrary.cart.controller;

import com.uliky.common.constant.AuthServerConstant;
import com.uliky.shouyiLibrary.cart.interceptor.CartInterceptor;
import com.uliky.shouyiLibrary.cart.service.CartService;
import com.uliky.shouyiLibrary.cart.vo.CartItemVo;
import com.uliky.shouyiLibrary.cart.vo.CartVo;
import com.uliky.shouyiLibrary.cart.vo.UserInfoTo;
import org.apache.commons.lang.math.IEEE754rUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Autowired
    private CartService cartService;
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        CartVo cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }
    /**
     * 查询购物车选中的商品
     */
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItemVo> getCurrentUserCartItems() {
        return cartService.getUserCartItems();
    }
    /**
     * 添加商品到购物车
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num ,
                            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.shouyiLibrary.com/addToCartSuccess.html";
    }
    /**
     * 添加成功页面跳转
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        //重定向后查询购物车信息
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItemVo);
        return "success";
    }
    /**
     * 商品选中状态修改
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer check) {
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.shouyiLibrary.com/cart.html";
    }
    /**
     * 商品数量修改
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.changeItemCount(skuId,num);
        return "redirect:http://cart.shouyiLibrary.com/cart.html";
    }
    /**
     * 删除商品
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.shouyiLibrary.com/cart.html";
    }
}
